package park.manage;

import com.sun.source.tree.CaseTree;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLOutput;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Function {
    int sumCarPosition = 1;//一共有500车位
    //用于存储车辆信息的集合
    TreeSet<Car> set = new TreeSet<>();
    //用于存储值班人员的信息
    HashSet<User> userSet = new HashSet<>();
    public  void functionTipsUser(){
        System.out.println("\t\t\t\t\t\t******************\033[31m欢迎使用停车场管理系统\033[0m***********************");
        System.out.println("\t\t\t\t\t\t******************\033[31m值班人员菜单\033[0m***********************");
        System.out.println("\t\t\t\t\t\t******************1.查看系统***********************");
        System.out.println("\t\t\t\t\t\t******************2.增加信息***********************");
        System.out.println("\t\t\t\t\t\t******************3.修改信息***********************");
        System.out.println("\t\t\t\t\t\t******************4.删除信息***********************");
        System.out.println("\t\t\t\t\t\t******************5.某车辆费用详情***********************");
        System.out.println("\t\t\t\t\t\t******************6.清空系统内容***********************");
        System.out.println("\t\t\t\t\t\t******************7.查看车主车辆状态***********************");
        System.out.println("\t\t\t\t\t\t******************8.剩余停车位数量***********************");
        System.out.println("\t\t\t\t\t\t******************0.退出系统***********************");
    }

    public  void functionTipsManager(){
        System.out.println("\t\t\t\t\t\t******************\033[31m欢迎使用停车场管理系统\033[0m***********************");
        System.out.println("\t\t\t\t\t\t******************\033[31m管理员菜单\033[0m***********************");
        System.out.println("\t\t\t\t\t\t******************1.查看系统***********************");
        System.out.println("\t\t\t\t\t\t******************2.增加信息***********************");
        System.out.println("\t\t\t\t\t\t******************3.修改信息***********************");
        System.out.println("\t\t\t\t\t\t******************4.删除信息***********************");
        System.out.println("\t\t\t\t\t\t******************5.某车辆费用详情***********************");
        System.out.println("\t\t\t\t\t\t******************6.清空系统内容***********************");
        System.out.println("\t\t\t\t\t\t******************7.查看车主车辆状态***********************");
        System.out.println("\t\t\t\t\t\t******************8.剩余停车位数量***********************");
        System.out.println("\t\t\t\t\t\t******************9.查看值班人员信息***********************");
        System.out.println("\t\t\t\t\t\t******************\033[31ma\033[0m.删除值班人员信息***********************");
        System.out.println("\t\t\t\t\t\t******************0.退出系统***********************");
    }
    public  void programEntranceUser(TreeSet<Car> set) throws IOException, ParseException {
        //给集合添加文本文件中的信息
        this.set = set;
        Scanner sc = new Scanner(System.in);
        //打印功能菜单
        functionTipsUser();
        //用于功能的选择
        char n;
        while(true)  {
            System.out.println("请选择功能：");
            n = sc.next().charAt(0);
            switch (n) {
                case '1': {
                    lookSystem();
                }
                break;
                case '2': {
                    addCar();
                }
                break;
                case '3':{
                    reviseInformation();
                }
                break;
                case '4':{
                    deleteInformation();
                }
                break;
                case '5':{
                    carExpenses();
                }
                break;
                case '6':{
                    clearInformation();
                }
                case '7':{
                    stationInformation();
                }
                break;
                case '8':{
                    carPositionInformation();
                }
                break;
                case '0': {
                    WriteFile.writeFile(set);
                    System.out.println("\033[31m欢迎下次使用！\033[0m");
                    System.exit(0);
                }
                default:
                    System.out.println("\033[31m输入错误！请重新输入  \033[0m");
            }
        }
    }

    public  void programEntranceManager(TreeSet<Car> set,HashSet<User> userSet) throws IOException, ParseException {
        //将文本文件中的信息交给集合
        this.set = set;
        this.userSet = userSet;
        Scanner sc = new Scanner(System.in);
        //打印功能菜单
        functionTipsManager();
        //用于功能的选择
        char n;
        while(true)  {
            System.out.println("请选择功能：");
            n = sc.next().charAt(0);
            switch (n) {
                case '1': {
                    lookSystem();
                }
                break;
                case '2': {
                    addCar();
                }
                break;
                case '3':{
                    reviseInformation();
                }
                break;
                case '4':{
                    deleteInformation();
                }
                break;
                case '5':{
                    carExpenses();
                }
                break;
                case '6':{
                    clearInformation();
                }
                case '7':{
                    stationInformation();
                }
                break;
                case '8':{
                    carPositionInformation();
                }
                break;
                case '9':{
                    lookStaffInformation();
                }
                break;
                case 'a':{
                    deleteStaffInformation();
                }
                break;
                case '0': {
                    WriteFile.writeFile(set);
                    System.out.println("\033[31m欢迎下次使用！\033[0m");
                    System.exit(0);
                }
                default:
                    System.out.println("\033[31m输入错误！请重新输入  \033[0m");
            }
        }
    }
    public  void lookSystem() throws FileNotFoundException, ParseException {
        //判断集合是否是空
        if(set.isEmpty()){
            System.out.println("此系统当前为空");
            return;
        }
        ReadFile.readFile(set);
        for(Car car : set){
            System.out.println(car);
        }
    }
    public void addCar() throws IOException {
        if(carPositionInformation()==0){
            System.out.println("\033[31m很抱歉！当前没有停车位了\033[0m");
            return ;
        }
        Scanner sc = new Scanner(System.in);
            System.out.println("请输入车主名，车牌号");
            String name = sc.next();
            String carNumber = sc.next();
            sc.nextLine();
            //名字的车主的名字必须是以非数字开头，2到9个字符
            if(!name.matches("[^\\d].{1,8}")){
                System.out.println("\033[31m添加失败！车主名字格式错误\033[0m");
                return;
            }
            //只匹配内地的车牌号码，车牌号码由一个汉字，一个字母和5到6位的车牌号组成
            if(!carNumber.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$")) {
                System.out.println("\033[31m添加失败！车牌号格式错误\033[0m");
                return;
            }
            set.add(new Car(name,carNumber));
            WriteFile.writeFile(set);
            System.out.println("\033[31m添加成功\033[0m");
    }
    public void reviseInformation() throws IOException {
        Scanner sc = new Scanner(System.in);
        if(set.isEmpty()){
            System.out.println("\033[31m当前系统是空！\033[0m");
            return ;
        }
        System.out.println("请输入要车主信息的车牌号");
        String reviseCarNumber = sc.nextLine();
        if(!reviseCarNumber.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$")) {
            System.out.println("\033[31m车牌号输入有误！\033[0m");
            return;
        }
        for(Car car : set){
            //如果找到了此车牌号码
            if(car.carNumber.equals(reviseCarNumber)){
                System.out.println("\033[31m请输入要修改的信息(只能修改车主和车牌号码)\033[0m");
                String name = sc.next();
                String carNumber = sc.next();
                if(!name.matches("[^\\d].{1,8}")){
                    System.out.println("\033[31m姓名输入有误！\033[0m");
                    return ;
                }
                if(!carNumber.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$")){
                    System.out.println("\033[31m车牌号码输入有误！\033[0m");
                    return ;
                }
                set.add(new Car(name,carNumber,car.entryTime));
                set.remove(car);
                WriteFile.writeFile(set);
                return ;
            }
        }
        //如果没有找到这个车牌号码
            System.out.println("\033[31m很抱歉！系统没有此车牌号码\033[0m");
    }
    public void deleteInformation() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("\033[31m请输入要删除车主的车牌号码\033[0m");
        String str = sc.nextLine();
        if(!str.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$")){
            System.out.println("\033[31m车牌号码输入有误\033[0m");
            return ;
        }
        for(Car car : set){
            if(car.carNumber.equals(str)){
                set.remove(car);
                WriteFile.writeFile(set);
                System.out.println("\033[31m删除成功！\033[0m");
                return ;
            }
        }
        System.out.println("\033[31m删除失败！系统没有此车牌号！\033[0m");
    }
    public void carExpenses(){
        Scanner sc = new Scanner(System.in);
        System.out.println("\033[31m请输入要查看费用车辆的车牌号码\033[0m");
        String str = sc.nextLine();
        if(!str.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$")){
            System.out.println("\033[31m车牌号码输入有误\033[0m");
            return ;
        }
        for(Car car : set){
            if(car.carNumber.equals(str)){
                long nowTime = System.currentTimeMillis();
                long entryTime = car.entryTime.getTime();
                int day=0,hour=0,min=0;
                day = (int)((nowTime-entryTime)/1000/60/60/24);
                hour = (int)(((nowTime-entryTime)-(long)(1000*24*60*60*day))/1000/60/60);
                min = (int)(((nowTime-entryTime)-(long)(1000*24*60*60*day)-(long)(hour*1000*60*60))/1000/60);
                System.out.println("车辆停放的时间是"+day+"天"+hour+"小时"+min+"分钟");
                double money = 0;
                if(day==0){
                    if(hour<=3){
                        money = hour*5;
                    }
                    else{
                        money = hour*10;
                    }
                }else {
                    money = 65;
                    if(hour<=3){
                        money += hour*5;
                    }
                    else{
                        money += hour*10;
                    }
                }
                System.out.println("产生的费用是："+money);
                return ;
            }
        }
        System.out.println("\033[31m查看费用失败！系统没有此车牌号！\033[0m");
    }

    public void carExpenses(String str){

        for(Car car : set){
            if(car.carNumber.equals(str)){
                long nowTime = System.currentTimeMillis();
                long entryTime = car.entryTime.getTime();
                int day=0,hour=0,min=0;
                day = (int)((nowTime-entryTime)/1000/60/60/24);
                hour = (int)(((nowTime-entryTime)-(long)(1000*24*60*60*day))/1000/60/60);
                min = (int)(((nowTime-entryTime)-(long)(1000*24*60*60*day)-(long)(hour*1000*60*60))/1000/60);
                System.out.print("车辆停放的时间是"+day+"天"+hour+"小时"+min+"分钟,");
                double money = 0;
                if(day==0){
                    if(hour<=3){
                        money = hour*5;
                    }
                    else{
                        money = hour*10;
                    }
                }else {
                    money = 65;
                    if(hour<=3){
                        money += hour*5;
                    }
                    else{
                        money += hour*10;
                    }
                }
                System.out.println("产生的费用是："+money);
                return ;
            }
        }
    }
    public void clearInformation() throws IOException {
        if(set.isEmpty()){
            System.out.println("\033[31m清空成功！\033[0m");
            return ;
        }
        Iterator<Car> it = set.iterator();
        while(it.hasNext()){
            Car car = it.next();
            it.remove();
        }
        WriteFile.writeFile(set);
        System.out.println("\033[31m清空成功！\033[0m");
    }
    public void stationInformation(){
        int count = 0;//记录当前姓名的车主有多少个
        if(set.isEmpty()){
            System.out.println("\033[31m当前系统是空！\033[0m");
            return ;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入车主的姓名");
        String str = sc.nextLine();
        if(!str.matches("[^\\d].{1,8}")){
            System.out.println("\033[31m姓名格式错误！\033[0m");
            return ;
        }
        for(Car car : set){
            if(car.name.equals(str)){
                System.out.print(car);
                System.out.print(",");
                carExpenses(car.carNumber);
                count++;
            }
        }
        if(count==0){
            System.out.println("\033[31m系统查无此人！\033[0m");
        }
    }
    public int carPositionInformation(){
        int count = 0;//当前已经使用了多少停车位
        if(set.isEmpty()){
            System.out.println("\033[31m还有"+sumCarPosition+"个车位\033[0m");
            return sumCarPosition;
        }
        for(Car car : set){
            count++;
        }
        System.out.println("\033[31m还有"+(sumCarPosition-count)+"个车位\033[0m");
        return sumCarPosition-count;
    }
    public void lookStaffInformation(){
        if(userSet.isEmpty()){
            System.out.println("\033[31m当前系统无值班人员信息\033[0m");
            return ;
        }
        for(User user : userSet){
            System.out.println(user);
        }
    }
    public void deleteStaffInformation(){
        Scanner sc = new Scanner(System.in);
        if(userSet.isEmpty()){
            System.out.println("\033[31m当前系统无值班人员信息\033[0m");
            return ;
        }
        System.out.println("请输入想要删除值班人员的姓名");
        String str = sc.nextLine();
        if(!str.matches("[^\\d].{1,8}")){
            System.out.println("\033[31m姓名格式错误！\033[0m");
            return ;
        }
        for(User user : userSet){
            if(user.getName().equals(str)){
                userSet.remove(user);
                System.out.println("\033[31m删除成功！\033[0m");
                return ;
            }
        }
        System.out.println("\033[31m删除失败！系统查无此人！\033[0m");
    }
}
